package com.sq.universe.ureport.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.ureport.entity.UreportEntity;
import  com.sq.universe.ureport.service.UreportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@RestController
@RequestMapping("/ureport")
@CrossOrigin
public class UreportController extends BaseController {

    @Autowired
    private UreportService ureportService;

    /**
    * 查询数据
    * @param ureportEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("ureport:ureport:query")
    public RespEntity query(UreportEntity ureportEntity){
        try {
            return new RespEntity("查询成功",ureportService.query(ureportEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param ureportEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("ureport:ureport:queryByPage")
    public RespEntity queryByPage(UreportEntity ureportEntity){
        try {
            PageHelper.startPage(ureportEntity.getPageNo(),ureportEntity.getPageSize());
            List<UreportEntity> ureportList = ureportService.query(ureportEntity);
            PageInfo<UreportEntity> pageInfo = new PageInfo<UreportEntity>(ureportList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param ureportEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("ureport:ureport:insert")
    public RespEntity insert(UreportEntity ureportEntity){
        try {
            ureportEntity.setUreportCreateTime(new Date());
            ureportService.insert(ureportEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param ureportEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("ureport:ureport:update")
    public RespEntity update(UreportEntity ureportEntity){
        try {
            ureportEntity.setUreportUpdateTime(new Date());
            ureportService.update(ureportEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param ureportEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("ureport:ureport:delete")
    public RespEntity delete(UreportEntity ureportEntity){
        try {
            if(ureportEntity.getUreportId() == null){
                return new RespEntity("删除失败");
            }
            ureportEntity.setUreportUpdateTime(new Date());
            ureportService.logicalDelete(ureportEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }

    /**
    * 根据ID查询
    * @param ureportEntity
    * @return
    */
    @PostMapping("/getById")
    @RequiresPermissions("ureport:ureport:getById")
    public RespEntity getById(UreportEntity ureportEntity){
        try {
            if(ureportEntity.getUreportId() == null){
                return new RespEntity("请输入id查询");
            }
            return new RespEntity("查询成功",ureportService.getById(ureportEntity.getUreportId()));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 根据实体查询
    * @param ureportEntity
    * @return
    */
    @PostMapping("/get")
    @RequiresPermissions("ureport:ureport:get")
    public RespEntity get(UreportEntity ureportEntity){
        try {
            return new RespEntity("查询成功",ureportService.get(ureportEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }
}